package com.jacoblyu.factoryrunner.ui.views

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.ArrowForward
import androidx.compose.material.icons.filled.Bluetooth
import androidx.compose.material.icons.filled.BluetoothSearching
import androidx.compose.material.icons.filled.Home
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.jacoblyu.factoryrunner.data.TestResultManager
import com.jacoblyu.factoryrunner.ui.theme.*

@Composable
fun BluetoothTestView(navController: NavController) {
    // 根据屏幕尺寸调整字体和间距
    val screenWidth = LocalConfiguration.current.screenWidthDp
    val screenHeight = LocalConfiguration.current.screenHeightDp
    
    // 根据屏幕尺寸调整字体大小
    val titleFontSize = if (screenWidth >= 1920) 48.sp else 32.sp
    val contentFontSize = if (screenWidth >= 1920) 36.sp else 24.sp
    val buttonFontSize = if (screenWidth >= 1920) 28.sp else 18.sp
    
    // 根据屏幕尺寸调整间距
    val spacing = if (screenWidth >= 1920) 32.dp else 24.dp
    val buttonHeight = if (screenWidth >= 1920) 80.dp else 60.dp
    val buttonWidth = if (screenWidth >= 1920) 200.dp else 150.dp
    
    // 初始化TestResultManager
    val context = LocalContext.current
    val testResultManager = remember { TestResultManager(context) }
    
    // 测试状态
    var isScanning by remember { mutableStateOf(false) }
    var bluetoothStatus by remember { mutableStateOf("未连接") }
    var deviceCount by remember { mutableStateOf(0) }
    var testResult by remember { mutableStateOf(testResultManager.getTestResult("蓝牙测试") ?: "未测试") }
    
    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(MaterialTheme.colorScheme.background)
    ) {
        // 标题栏
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(spacing),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
        ) {
            // 上一步按钮
            // Home按钮
            IconButton(
                onClick = { navController.navigate("home") }
            ) {
                Icon(
                    imageVector = Icons.Filled.Home,
                    contentDescription = "返回主页",
                    tint = MaterialTheme.colorScheme.primary,
                    modifier = Modifier.size(if (screenWidth >= 1920) 48.dp else 36.dp)
                )
            }
            
            // 标题
            Text(
                text = "蓝牙测试",
                fontSize = titleFontSize,
                fontWeight = FontWeight.Bold,
                color = MaterialTheme.colorScheme.primary
            )
            
            // 下一步按钮
            IconButton(
                onClick = { navController.navigate("light_test") }
            ) {
                Icon(
                    imageVector = Icons.Default.ArrowForward,
                    contentDescription = "下一步",
                    tint = MaterialTheme.colorScheme.primary,
                    modifier = Modifier.size(if (screenWidth >= 1920) 48.dp else 36.dp)
                )
            }
        }
        
        // 内容区域
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(spacing),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {
            // 蓝牙测试内容
            Card(
                modifier = Modifier
                    .fillMaxWidth(0.8f)
                    .height((screenHeight * 0.5f).dp),
                shape = RoundedCornerShape(16.dp),
                colors = CardDefaults.cardColors(
                    containerColor = MaterialTheme.colorScheme.surface
                ),
                elevation = CardDefaults.cardElevation(
                    defaultElevation = 8.dp
                )
            ) {
                Column(
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(spacing),
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.SpaceEvenly
                ) {
                    // 蓝牙图标和状态
                    Icon(
                        imageVector = if (isScanning) Icons.Default.BluetoothSearching else Icons.Default.Bluetooth,
                        contentDescription = "蓝牙图标",
                        tint = if (bluetoothStatus == "已连接") MaterialTheme.colorScheme.primary 
                              else MaterialTheme.colorScheme.onSurface.copy(alpha = 0.6f),
                        modifier = Modifier.size(if (screenWidth >= 1920) 120.dp else 80.dp)
                    )
                    
                    // 连接状态
                    Text(
                        text = "连接状态: $bluetoothStatus",
                        fontSize = contentFontSize,
                        fontWeight = FontWeight.Bold,
                        color = if (bluetoothStatus == "已连接") Color.Green 
                               else if (bluetoothStatus == "扫描中...") Color.Blue 
                               else MaterialTheme.colorScheme.onSurface,
                        textAlign = TextAlign.Center
                    )
                    
                    // 发现的设备数量
                    Text(
                        text = "发现设备数量: $deviceCount",
                        fontSize = contentFontSize,
                        fontWeight = FontWeight.Medium,
                        color = MaterialTheme.colorScheme.onSurface,
                        textAlign = TextAlign.Center
                    )
                    
                    Spacer(modifier = Modifier.height(spacing))
                    
                    // 扫描按钮
                    Button(
                        onClick = { 
                            isScanning = !isScanning
                            if (isScanning) {
                                bluetoothStatus = "扫描中..."
                                deviceCount = 3
                                testResult = "测试中..."
                            } else {
                                bluetoothStatus = "已连接"
                                testResult = "已测试"
                            }
                        },
                        modifier = Modifier
                            .width(buttonWidth * 1.5f)
                            .height(buttonHeight),
                        colors = ButtonDefaults.buttonColors(
                            containerColor = if (isScanning) MaterialTheme.colorScheme.secondary 
                                           else MaterialTheme.colorScheme.primary
                        )
                    ) {
                        Text(
                            text = if (isScanning) "停止扫描" else "扫描蓝牙设备",
                            fontSize = buttonFontSize,
                            color = MaterialTheme.colorScheme.onPrimary
                        )
                    }
                }
            }
            
            Spacer(modifier = Modifier.height(spacing))
            
            // 按钮区域
            Row(
                horizontalArrangement = Arrangement.spacedBy(spacing),
                verticalAlignment = Alignment.CenterVertically
            ) {
                // 不通过按钮
                Button(
                    onClick = { 
                        // 保存测试结果
                        testResultManager.saveTestResult("蓝牙测试", "失败")
                        testResult = "失败"
                        // 导航到下一个测试页面
                        navController.navigate("light_test?result=失败&test=蓝牙测试")
                    },
                    modifier = Modifier
                        .width(buttonWidth)
                        .height(buttonHeight),
                    colors = ButtonDefaults.buttonColors(
                        containerColor = MaterialTheme.colorScheme.error
                    )
                ) {
                    Text(
                        text = "不通过",
                        fontSize = buttonFontSize,
                        color = MaterialTheme.colorScheme.onError
                    )
                }
                
                // 通过按钮
                Button(
                    onClick = { 
                        // 保存测试结果
                        testResultManager.saveTestResult("蓝牙测试", "通过")
                        testResult = "通过"
                        // 导航到下一个测试页面
                        navController.navigate("light_test?result=通过&test=蓝牙测试")
                    },
                    modifier = Modifier
                        .width(buttonWidth)
                        .height(buttonHeight),
                    colors = ButtonDefaults.buttonColors(
                        containerColor = MaterialTheme.colorScheme.primary
                    )
                ) {
                    Text(
                        text = "通过",
                        fontSize = buttonFontSize,
                        color = MaterialTheme.colorScheme.onPrimary
                    )
                }
            }
        }
    }
}

@Preview(showBackground = true)
@Composable
fun BluetoothTestPreview() {
    FactoryrunnerTheme {
        BluetoothTestView(rememberNavController())
    }
}

@Preview(showBackground = true, widthDp = 1920, heightDp = 1200)
@Composable
fun BluetoothTestLargePreview() {
    FactoryrunnerTheme {
        BluetoothTestView(rememberNavController())
    }
}